package ca.uwo.android.util;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLConnection;

import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.widget.ImageView;

public class DownloadTask extends AsyncTask<String, Void, String> {

   private int position;
   private ImageView imageView;
   private Drawable placeholder;
   
	// Academic event - school_dates_feed
	private static final String URL1 = "https://events.uwo.ca/cgi-bin/events.pl?CalendarName=OfficeoftheRegistrar&amp;Op=RSS";
	// Western Main Events - western_events_feed
	private static final String URL2 = "https://events.uwo.ca/cgi-bin/events.pl?CalendarName=WesternEvents&amp;Op=RSS";
	// Service Interruption Events - service_interruptions_feed
	private static final String URL3 = "https://events.uwo.ca/cgi-bin/events.pl?CalendarName=PhysicalPlantServiceInterruptions&amp;Op=RSS";
	// Alumni Events - alumni_events_feed
	private static final String URL4 = "http://pipes.yahoo.com/pipes/pipe.run?_id=dc1d097c6cef2b81ab1c6a98b661269c&_render=rss";
	// Mustang Events - mustangs_schedule_link
	private static final String URL5 = "http://www.westernmustangs.ca/services/calendar.ashx/calendar.rss?sport_id=";
  
	private HttpURLConnection connection;
	
	private String packageName;

   public DownloadTask(String packageName) {
	   this.packageName = packageName;
   }
	
	   private void write() throws IOException {
		      if (FileUtil.isExternalStorageWritable()) {
		         File dir = FileUtil.getExternalFilesDirAllApiLevels(packageName);
		         File file = new File(dir, "alumni.xml");
		         FileUtil.writeStringAsFile(readTextFromServer(), file);
		      }
		   }

   @Override
   protected String doInBackground(String... inputUrls) {
	   
       try {
		URL url = new URL(URL4);
		   connection = (HttpURLConnection) url.openConnection();
		   connection.setRequestMethod("GET");
		   connection.setRequestProperty("Accept", "text/xml");
		   connection.setReadTimeout(10000);
		   connection.setConnectTimeout(10000);
		   
		   connection.connect();
		   int statusCode = connection.getResponseCode();
		   if (statusCode != HttpURLConnection.HTTP_OK) {
		      return "Error: Failed getting update notes";
		   }
		   write();
		   
	} catch (MalformedURLException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	} catch (ProtocolException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	} catch (IOException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	}
	return "";

   }
   
   private String readTextFromServer() throws IOException {
	      InputStreamReader isr =
	               new InputStreamReader(connection.getInputStream());
	      BufferedReader br = new BufferedReader(isr);

	      StringBuilder sb = new StringBuilder();
	      String line = br.readLine();
	      while (line != null) {
	         sb.append(line + "\n");
	         line = br.readLine();
	      }
	      return sb.toString();
	   }

   @Override
   protected void onPostExecute(String result) {

   }
}
